﻿namespace EasyWeb
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using EasyWeb.Data;
    using EasyWeb.Design;

    [EWDisplayName(Name = "FieldDefinitionDateTimeOffset", ResourceType = typeof(EWFieldDefinitionResources))]
    public class EWFieldDefinitionDateTimeOffset : EWFieldDefinition
    {
        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowDefaultValue")]
        public override bool AllowDefaultValue
        {
            get { return true; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowUnique")]
        public override bool AllowUnique
        {
            get { return true; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_DefaultValue")]
        public override object DefaultValue
        {
            get { return this.StateBag.Get<DateTimeOffset?>("DefaultValue"); }
            set { this.StateBag.Set("DefaultValue", (DateTimeOffset?)value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionDateTimeOffset_IsNowDefaultValue")]
        public bool IsNowDefaultValue
        {
            get { return this.StateBag.Get<bool>("IsNowDefaultValue"); }
            set { this.StateBag.Set("IsNowDefaultValue", value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionDateTimeOffset_DisplayTimeZone")]
        public bool DisplayTimeZone
        {
            get { return this.StateBag.Get<bool>("DisplayTimeZone"); }
            set { this.StateBag.Set("DisplayTimeZone", value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionDateTimeOffset_DefaultUtcOffset")]
        public TimeSpan? DefaultUtcOffset
        {
            get { return this.StateBag.Get<TimeSpan?>("DefaultUtcOffset"); }
            set { this.StateBag.Set("DefaultUtcOffset", value); }
        }

        public override EWFieldDefinitionColumnCollection CreateFieldDefinitionColumns()
        {
            return new EWFieldDefinitionColumnCollection()
            {
                new EWFieldDefinitionColumn()
                {
                    Name = this.InternalName,
                    IsNullable = this.AllowNull,
                    IsUnique = this.AllowUnique && this.IsUnique,
                    Type = DbType.DateTimeOffset
                }
            };
        }

        public override void Deserialize(EWSerializationInfo info)
        {
            base.Deserialize(info);

            this.IsNowDefaultValue = info.GetBoolean("IsNowDefaultValue");
            this.DisplayTimeZone = info.GetBoolean("DisplayTimeZone");
            this.DefaultUtcOffset = info.GetNullableTimeSpan("DefaultUtcOffset");
            this.DefaultValue = info.GetNullableDateTimeOffset("DefaultValue");
        }

        public override void Serialize(EWSerializationInfo info)
        {
            base.Serialize(info);

            info.AddValue("IsNowDefaultValue", this.IsNowDefaultValue);
            info.AddValue("DisplayTimeZone", this.DisplayTimeZone);
            info.AddValue("DefaultUtcOffset", this.DefaultUtcOffset);
            info.AddValue("DefaultValue", (DateTimeOffset?)this.DefaultValue);
        }
    }
}
